package com.dmall.leetcode303;

public class NumArray2 {
    private int[] data;

    public NumArray2(int[] nums) {
        data = new int[nums.length + 1];
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
            data[i + 1] = sum;
        }
    }

    public int sumRange(int i, int j) {
        return data[j + 1] - data[i];
    }

    public static void main(String[] args) {
        int[] nums = {-2, 0, 3, -5, 2, -1};
        NumArray2 numArray2 = new NumArray2(nums);
        System.out.println(numArray2.sumRange(0 , 2));
    }
}
